package panel

import "freetype-go.googlecode.com/hg/freetype/raster"

type Shelf struct {
    Common
    elements []View
}

func NewShelf(elements ...View) View {
    shelf := &Shelf{Common{}, elements}
    for _, e := range elements { e.Parent(shelf) }
    return shelf
}

func (s *Shelf) FocusForClick(xy Point) View {
	width := raster.Fix32(int(s.WH.X) / len(s.elements))
	x := xy.X
	for _, e := range s.elements {
		if x < width { return e.FocusForClick(Point{x, s.WH.Y}) }
		x -= width
	}
	return s
}

func (s *Shelf) At(xy Point) View {
	width := raster.Fix32(int(s.WH.X) / len(s.elements))
	x := xy.X
	for _, e := range s.elements {
		if x < width { return e.At(Point{x, xy.Y}) }
		x -= width
	}
	return s
}

func (s *Shelf) Key(rune int) View {
    return nil
}

func (s *Shelf) Refresh() {
	// Log("Shelf.Refresh")
	for _, e := range s.elements { e.Refresh() }
}

func (s *Shelf) Resize(wh Size, offset Point) {
    s.Common.Resize(wh, offset)
	w := raster.Fix32(int(s.WH.X) / len(s.elements))
    dx := raster.Fix32(0)
    for _, e := range s.elements {
    	e.Resize(Size{w, s.WH.Y}, Point{dx, 0})
    	dx += w
    }
}

